'use strict';

// Formas de Pagamento
angular.module('simon.controllers').controller('CondPagtoCtrl', ['$scope', 'CondPagtoFactory', 'CondPagtoIdFactory', 'FormPagtoFactory', 'AlertFactory', 'ModalFactory',
  function ($scope, CondPagtoFactory, CondPagtoIdFactory, FormPagtoFactory, AlertFactory, ModalFactory) {
	/* callback for ng-click 'create': */
    $scope.create = function () {
		$scope.isNew = true;
		$scope.item = {formaPagamento: '', parcelas: '', valorMinimo: '', diasVencimento: ''};
    };
	
	/* callback for ng-click 'edit': */
    $scope.edit = function (item) {
		$scope.isNew = false;
		$scope.item = item;
    };
    
    /* callback for ng-click 'insert': */
    $scope.insert = function () {
    	$scope.item.valorMinimo = $('#valorMinimo').maskMoney('unmasked')[0];
    	$scope.item = CondPagtoFactory.create($scope.item, 
    		function (success) { $scope.itens.push($scope.item); $scope.$broadcast("RESET_FORM"); AlertFactory.add("success", "Registro salvo com sucesso!"); },
    		function (error) { AlertFactory.add("danger", "Falha ao inserir o registro!"); }
    	);
    };
	
	/* callback for ng-click 'update': */
    $scope.update = function () {
    	$scope.item.valorMinimo = $('#valorMinimo').maskMoney('unmasked')[0];
    	$scope.item = CondPagtoIdFactory.update($scope.item, 
			function (success) { $scope.$broadcast("RESET_FORM"); AlertFactory.add("success", "Registro salvo com sucesso!"); },
			function (error) { AlertFactory.add("danger", "Falha ao atualizar o registro!"); }
		);
    };
    
    /* callback for ng-click 'remove': */
    $scope.remove = function (rowId, idx) {
    	var modal = ModalFactory.confirm();
    	modal.result.then(function () {
    		CondPagtoIdFactory.remove({ id: rowId }, 
				function (success) { $scope.itens.splice(idx, 1); AlertFactory.add("success", "Registro removido com sucesso!"); },
	    		function (error) { AlertFactory.add("danger", "Falha ao excluir o registro!"); }
			);
	    });
    };

    $scope.itens = CondPagtoFactory.query();
    $scope.formsPagto = FormPagtoFactory.query();
  }]);
